import Vue from 'vue'
import Router from 'vue-router'
import Login from '@/views/Login.vue'
import Dashboard from '@/views/Dashboard.vue'
import Products from '@/views/Products.vue'
import Orders from '@/views/Orders.vue'
import Statistics from '@/views/Statistics.vue'
import Customers from '@/views/Customers.vue'
import Couriers from '@/views/Couriers.vue'

Vue.use(Router)

const router = new Router({
mode: 'history',
routes: [
{ path: '/', redirect: '/login' },
{ path: '/login', component: Login },
{
path: '/dashboard',
component: Dashboard,
meta: { requiresAuth: true },
children: [
{ path: 'products', component: Products },
{ path: 'orders', component: Orders },
{ path: 'statistics', component: Statistics },
{ path: 'customers', component: Customers },
{ path: 'couriers', component: Couriers },
{ path: '', redirect: 'products' }
]
},
{ path: '*', redirect: '/login' }
]
})

router.beforeEach((to, from, next) => {
const isLogin = localStorage.getItem('isLogin') === 'true'
if (to.matched.some(r => r.meta.requiresAuth) && !isLogin) {
next('/login')
} else {
next()
}
})

export default router